$(function () {
    getSelectMess();
    getUserList2();
});
/*重置*/
function resetClick(){
    $(".dis-inebl input").val("");
    $("#userState option:selected").val("全部");
    $("#userRole option:selected").val("全部");
    getUserList();
}
/*查询*/
function getUserList(init){
    JY.Model.loading();
	$("#ticktTable tbody").html("");
    $("#baseTable2 tbody").html("");
    $("#baseTable2 tbody").append("<tr><td colspan='6' class='center'>没有相关数据</td></tr>");
    var pageNum=$("#baseForm .pageNum").val();
    var pageSize=$("#baseForm .pageSize").val();
	var userName =$("#userName").val().trim();
	var userTel =$("#userTel").val().trim();
	var userState=$("#userState option:selected").val();
    var userRole=$("#userRole option:selected").val();
    if(userState=="全部"){
        userState="";
    }
    if(userRole=="全部"){
        userRole="";
    }
	$.ajax({
		url : jypath + "/user/getUserList",
		type : "post",
		data:{
            "pageNum":pageNum,
            "pageSize":pageSize,
            "nickname":userName,
            "userPhone":userTel,
            "state":userState,
            "type":userRole,
		},
		jsonType : "json",
		success : function(data) {
		    console.log(data)
            var data=data instanceof Object ? data : JSON.parse(data);
            var html="";
            var list=data.obj;
            var results=list.results;
            var pageNum=list.pageNum,
                pageSize=list.pageSize,
                totalRecord=list.totalRecord;
           if(results!=null&&results.length>0){
                var leng=(pageNum-1)*pageSize;//计算序号
                for(var i = 0;i<results.length;i++){
                    var l=results[i];
                    html+="<tr class='tr_color'>";
                    html+="<td class='center ver-m'><label><input type='checkbox' name='ids' value='"+l.userId+"' roleType='"+l.type+"' class='ace' /> <span class='lbl'></span></label></td>";
                    html+="<td class='center'>"+l.nickname+"</td>";
                    html+="<td class='center'>"+l.userPhone+"</td>";
                    if(l.type==1){
                        html+="<td class='center'>医生</td>";
                    }else if(l.type==0){
                        html+="<td class='center'>普通用户</td>";
                    }
                    if(l.registerChannel==null||l.registerChannel==undefined||l.registerChannel==""){
                        html+="<td class='center'>暂无</td>";
                    }else{
                        html+="<td class='center'>"+l.registerChannel+"</td>";
                    }
                    if(l.address==null||l.address==undefined||l.address==""){
                        html+="<td class='center'>暂无</td>";
                    }else{
                        html+="<td class='center'>"+l.address+"</td>";
                    }
                    html+="<td class='center'>"+l.userPoint+"</td>";
                    if(l.state==0){
                        html+="<td class='center'>失效</td>";
                    }else if(l.state==1){
                        html+="<td class='center'>有效</td>";
                    }
                    html+="<td class='center'><a onclick='getUserList2("+l.userId+",this)'>查看用户孩子</a></td>";
                    html+="</tr>";
                }
                $("#ticktTable tbody").append(html);
                JY.Page.setPage("baseForm","pageing",pageSize,pageNum,totalRecord,"getUserList");
            }else{
                html+="<tr><td colspan='8' class='center'>没有相关数据</td></tr>";
                $("#ticktTable tbody").append(html);
                $("#baseForm ul").empty();//清空分页
            }
            JY.Model.loadingClose();
		}
	})
    ;
}
//获取医生等级
function getSelectMess(){
    JY.Model.loading();
    $.ajax({
        url : jypath + "/askDoctor/queryYmDoctorLevel",
        type : "post",
        jsonType : "json",
        success : function(data) {
            if(data.obj.length>0){
                var html="";
                for(var i=0;i<data.obj.length;i++){
                     html+="<option value='"+data.obj[i].levelId+"'>"+data.obj[i].levetName+"</option>"
                }
                $("#doctorLevels").append(html);
                $("#doctorLevel").append(html);
            }
            getConsulType()
        }
    })
}
//查询咨询类型
function getConsulType(){
    $.ajax({
        url : jypath + "/askDoctor/queryYmDouctorConsultType",
        type : "post",
        jsonType : "json",
        success : function(data) {
            if(data.obj.length>0){
                var html="";
                for(var i=0;i<data.obj.length;i++){
                    html+="<option value='"+data.obj[i].douctorConsultTypeId+"'>"+data.obj[i].douctorConsultTypeName+"</option>"
                }
                $("#douctorConsultTypeName").append(html);
                $("#douctorConsultTypeNames").append(html);
            }
        }
    })
    getStation();
}
/*查询服务站*/
function getStation(){
    $.ajax({
        url : jypath + "/vaccinestation/queryVaccineStation",
        type : "post",
        data:{
            "pageNum":1,
            "pageSize":10000,
        },
        jsonType : "json",
        success : function(data) {
            console.log(data)
            console.log("获取服务站信息");
            JY.Model.loadingClose();
            if(data.obj.results!=null&&data.obj.results.length>0){
                var html="";
                for(var i=0;i<data.obj.results.length;i++){
                    var k=data.obj.results[i];
                    html+="<option value='"+k.stationId+"'>"+k.stationName+"</option>"
                }
                $("#stationAddr").append(html);
                $("#stationAddrs").append(html);
            }
        }
    })
    getUserList();
}
/*查询孩子信息*/
function getUserList2(userId,obj){
    $(obj).parents(".tr_color").addClass("hascolor").siblings("tr").removeClass("hascolor");
    $("#baseTable2 tbody").html("");
    var html="";
    if(userId==undefined||userId==null||userId==""){
        html+="<tr><td colspan='6' class='center'>没有相关数据</td></tr>";
        $("#baseTable2 tbody").append(html);
    }else{
        $.ajax({
            url : jypath + "/userChild/getUserChild",
            type : "post",
            data:{
                "userId":userId,
            },
            jsonType : "json",
            success : function(data) {
                console.log(data)
                console.log("获取孩子信息")
                if(data.obj.length>0&&data.obj!=null){
                   html="";
                   for(var i=0;i<data.obj.length;i++){
                       var g=data.obj[i];
                       html+="<tr>"
                       html+="<td class='center'>"+(i+1)+"</td>"
                       if(g.childRealname==""||g.childRealname==undefined||g.childRealname==null){
                           g.childRealname="暂无"
                       }
                       html+="<td class='center'>"+g.childRealname+"</td>"
                       if(g.childNickname==""||g.childNickname==undefined||g.childNickname==null){
                           g.childNickname="暂无"
                       }
                       html+="<td class='center'>"+g.childNickname+"</td>"
                       if(g.childSex==1){
                           html+="<td class='center'>男</td>"
                       }else if(g.childSex==2){
                           html+="<td class='center'>女</td>"
                       }
                       html+="<td class='center'>"+g.childBirthday+"</td>"
                       if(g.relationship==""||g.relationship=="null"||g.relationship==null){
                           g.relationship="未知"
                       }
                       html+="<td class='center'>"+g.relationship+"</td>"
                       html+="</tr>"
                   }
                }else{
                    html+="<tr><td colspan='6' class='center'>没有相关数据</td></tr>";
                }
                $("#baseTable2 tbody").append(html);
            }
        })
    }
}
//查看详情
function checkClick(){
    var chks =[];
    $('#ticktTable input[name="ids"]:checked').each(function(){
        chks.push($(this).val());
    });
    if(chks.length==0) {
        JY.Model.info("您没有选择任何内容!");
    }else if(chks.length==1){
        var type=$('#ticktTable input[name="ids"]:checked').attr("roleType");
        if(type==1){
            var userId=chks[0];
            $.ajax({
                url : jypath + "/askDoctor/queryDoctorInfo",
                type : "post",
                data:{
                    "userId":userId
                },
                jsonType : "json",
                success : function(data) {
                    $("#doctors input[name='doctorRealName']").val(data.obj.doctorRealName);
                    $("#doctorLevel").find("option[value = '"+data.obj.doctorLevelId+"']").attr("selected","selected");
                    $("#douctorConsultTypeName").find("option[text = '"+data.obj.douctorConsultTypeName+"']").attr("selected","selected");
                    $("#doctorTags").find("option[value = '"+data.obj.doctorTags+"']").attr("selected","selected");
                    if(data.obj.recommend==0){
                        $("#doctors .numTime").addClass("hide");
                    }else if(data.obj.recommend==1){
                        $("#doctors .numTime").removeClass("hide");
                    }
                    $("#grooms").find("option[value = '"+data.obj.recommend+"']").attr("selected","selected");
                    $("#doctors textarea[name='des']").val(data.obj.des);
                    $("#doctors textarea[name='evaluate']").val(data.obj.doctorAssess);
                    if(data.obj.quota!=undefined&&data.obj.quota!=null&&data.obj.quota!=""){
                        $("#doctors input[name='quota']").val(data.obj.quota);
                    }
                    $("#stationAddrs").find("option[value = '"+data.obj.stationId+"']").attr("selected","selected");
                }
            })
            JY.Model.check("auForm_check","医生信息",function(){

            });
        }else{
            JY.Model.info("您当前选择的用户不是医生角色!");
        }
    }else if(chks.length>1){
        JY.Model.info("一次只能编辑一条信息!");
    }
}
//是否推荐
function changeGroom(){
    var groom=$("#groomsSel option:selected").val();
    if(groom==1){
         $(".numTime").removeClass("hide")
    }else if(groom==0){
         $(".numTime").addClass("hide")
    }
}
// 启用禁用
function enAbleClick(type){
    var chks =[];
    $('#ticktTable input[name="ids"]:checked').each(function(){
        chks.push($(this).val());
    });
    if(chks.length==0) {
        JY.Model.info("您没有选择任何内容!");
    }else if(chks.length>0){
        var states="";
        if(type==1){
            //启用
            states=1;
        }else if(type==0){
            //禁用
            states=0;
        }
        JY.Model.message("auForm_edit","修改状态",function(){
            var that = $(this);
            $.ajax({
                url : jypath + "/user/updateUserState",
                type : "post",
                data:{
                    "state":states,
                    "userId":chks
                },
                jsonType : "json",
                success : function(data) {
                   console.log(data);
                   if(data.res==1){
                       that.dialog("close");
                       JY.Model.info("操作成功!",function(){getUserList();});
                   }
                }
            })
        });
    }
}
//医生新增编辑信息
function changeRole(type){
  if(type==0){
     $(".doctorMessage").addClass("hide")
  }else if(type==1){
      $(".doctorMessage").removeClass("hide")
  }
}
//修改角色
function edit(){
    clearData();
    var chks =[];
    var doctorId="";
    $('#ticktTable input[name="ids"]:checked').each(function(){
        chks.push($(this).val());
    });
    if(chks.length==0) {
        JY.Model.info("您没有选择任何内容!");
    }else if(chks.length==1){
       var type="";
       type=$('#ticktTable input[name="ids"]:checked').attr("roleType");
       if(type==0){
           $("#auForm_edits .doctorMessage").addClass("hide")
       }else{
           $("#auForm_edits .doctorMessage").removeClass("hide");
       }
        var userId=chks[0];
        $.ajax({
            url : jypath + "/askDoctor/queryDoctorInfo",
            type : "post",
            data:{
                "userId":userId
            },
            jsonType : "json",
            success : function(data) {
                console.log(data)
                console.log("获取医生信息")
                if(data.obj==null){

                }else{
                    doctorId=data.obj.doctorId;
                    $("#generateForm input[name='doctorRealName']").val(data.obj.doctorRealName);
                    $("#doctorLevels").find("option[value = '"+data.obj.doctorLevelId+"']").attr("selected","selected");
                    $("#douctorConsultTypeNames").find("option[value = '"+data.obj.douctorConsultTypeId+"']").attr("selected","selected");
                    $("#doctorTagss").find("option[value = '"+data.obj.doctorTags+"']").attr("selected","selected");
                    if(data.obj.quota!=undefined&&data.obj.quota!=null&&data.obj.quota!=""){
                        $("#generateForm input[name='quota']").val(data.obj.quota);
                    }
                    if(data.obj.recommend==0){
                        $("#generateForm .numTime").addClass("hide");
                    }else if(data.obj.recommend==1){
                        $("#generateForm .numTime").removeClass("hide");
                    }
                    $("#groomsSel").find("option[value = '"+data.obj.recommend+"']").attr("selected","selected");
                    $("#generateForm textarea[name='des']").val(data.obj.des);
                    $("#generateForm textarea[name='evaluate']").val(data.obj.doctorAssess);
                    $("#stationAddr").find("option[value = '"+data.obj.stationId+"']").attr("selected","selected");
                }
            }
        })
        $('input:radio').attr("checked",false);
        $('input:radio').each(function(){
           if($(this).val()==type||$(this).val()==parseInt(type)){
               $(this).prop('checked', 'checked')
           }
        });
        JY.Model.edit("auForm_edits","修改角色",function(){
            var that = $(this);
            var types=$('.roles:radio:checked').val();
            var userId=chks[0];
            if(types==1){
               if(JY.Validate.form("generateForm")){
                   if($("#stationAddr").find("option:selected").val()==""||$("#stationAddr").find("option:selected").val()==null){
                       JY.Model.info("请先选择服务站点，若暂无相关站点信息，请先新增站点信息！");
                   }else if($("#generateForm textarea").val()==""||$("#generateForm textarea").val()==null){
                       JY.Model.info("请先完善相关信息！");
                   }else {
                       if (doctorId != "" && doctorId != undefined && doctorId != null) {
                           //编辑
                           $.ajax({
                               url: jypath + "/askDoctor/updateYmDouctorInfo",
                               type: "post",
                               data: {
                                   "userId": userId,
                                   "doctorId": doctorId,
                                   "doctorRealName": $("#generateForm input[name='doctorRealName']").val(),
                                   "doctorLevelId": $("#doctorLevels").find("option:selected").val(),
                                   "douctorConsultTypeId": $("#douctorConsultTypeNames").find("option:selected").val(),
                                   "doctorTags": $("#doctorTagss").find("option:selected").val(),
                                   "recommend": $("#groomsSel").find("option:selected").val(),
                                   "douctorConsultTypeName": $("#douctorConsultTypeNames").find("option:selected").text(),
                                   "levetName": $("#doctorLevels").find("option:selected").text(),
                                   "des": $("#generateForm textarea[name='des']").val(),
                                   "doctorAssess": $("#generateForm textarea[name='evaluate']").val(),
                                   "quota": $("#generateForm input[name='quota']").val(),
                                   "totalQuota": $("#generateForm input[name='quota']").val(),
                                   "stationId": $("#stationAddr").find("option:selected").val(),
                                   "workStart": $("#serverStartTime").find("option:selected").text() + ":00",
                                   "workEnd": $("#serverEndTime").find("option:selected").text() + ":00"
                               },
                               jsonType: "json",
                               success: function (data) {
                                   if (data.res == 1) {
                                       that.dialog("close");
                                       JY.Model.info("操作成功!", function () {
                                           getUserList();
                                       });
                                   } else {
                                       that.dialog("close");
                                       JY.Model.info(data.resMsg);
                                   }
                               }
                           })
                       } else {
                           //新增
                           $(".masks").removeClass("hide");
                           console.log('asfdgdf')
                           $.ajax({
                               url: jypath + "/askDoctor/insertAskDoctor",
                               type: "post",
                               data: {
                                   "userId": userId,
                                   "doctorRealName": $("#generateForm input[name='doctorRealName']").val(),
                                   "doctorLevelId": $("#doctorLevels").find("option:selected").val(),
                                   "douctorConsultTypeId": $("#douctorConsultTypeNames").find("option:selected").val(),
                                   "doctorTags": $("#doctorTagss").find("option:selected").val(),
                                   "recommend": $("#groomsSel").find("option:selected").val(),
                                   "douctorConsultTypeName": $("#douctorConsultTypeNames").find("option:selected").text(),
                                   "levetName": $("#doctorLevels").find("option:selected").text(),
                                   "des": $("#generateForm textarea[name='des']").val(),
                                   "doctorAssess": $("#generateForm textarea[name='evaluate']").val(),
                                   "quota": $("#generateForm input[name='quota']").val(),
                                   "totalQuota": $("#generateForm input[name='quota']").val(),
                                   "stationId": $("#stationAddr").find("option:selected").val(),
                                   "workStart": $("#serverStartTime").find("option:selected").text()+":00",
                                   "workEnd": $("#serverEndTime").find("option:selected").text()+":00"
                               },
                               jsonType: "json",
                               success: function (data) {
                                   if (data.res == 1) {
                                       that.dialog("close");
                                       $(".masks").addClass("hide");
                                       JY.Model.info("操作成功!", function () {
                                           getUserList();
                                       });
                                   } else {
                                       that.dialog("close");
                                       $(".masks").addClass("hide");
                                       JY.Model.info(data.resMsg);
                                   }
                               }
                           })
                       }
                   }
               }
            }else if(types==0){
                $.ajax({
                    url : jypath + "/user/updateUser",
                    type : "post",
                    data:{
                        "type":0,
                        "userId":userId
                    },
                    jsonType : "json",
                    success : function(data) {
                        console.log(data);
                        if(data.res==1){
                            that.dialog("close");
                            JY.Model.info("操作成功!",function(){getUserList();});
                        }
                    }
                })
            }
        });
    }else if(chks.length>1){
        JY.Model.info("一次只能编辑一条信息!");
    }
}
function clearData(){
    $("#generateForm input[name='doctorRealName']").val("");
    $("#doctorLevels").find("option[value = '1']").prop("selected","selected");
    $("#douctorConsultTypeNames").find("option[value = '0']").attr("selected","selected");
    $("#doctorTagss").find("option[value = '1']").prop("selected","selected");
    $("#generateForm .numTime").addClass("hide");
    $("#groomsSel").find("option[text = '否']").prop("selected","selected");
    $("#generateForm .des").val("");
    $("#stationAddrs").find("option[text = '未选择']").attr("selected","selected");
    $("#stationAddr").find("option[text = '未选择']").attr("selected","selected");
}
